package lumis.portal.channel.acl.serialization;

import java.util.EnumSet;

import lumis.portal.PortalException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.channel.ChannelConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.manager.ManagerFactoryInternal;
import lumis.portal.page.PageConfig;
import lumis.portal.serialization.BaseSerializationContext;
import lumis.portal.serialization.ISerializationMessageConfigNodeBuilder;
import lumis.portal.serialization.SerializationContext;
import lumis.portal.serialization.SerializationMessageConfigNode;
import lumis.portal.serialization.SerializationMessageConfigNode.RecursionStopPoints;
import lumis.util.security.acl.AccessControlList;
import lumis.util.security.acl.IAclManager;
import lumis.util.security.acl.serialization.AccessControlListSerializableConfig;
import lumis.util.security.acl.serialization.AccessControlListSerializationMessageConfig;
import lumis.util.security.acl.serialization.AclSerializationManager;

/**
 * Manager for Channel's Acl serialization
 *
 * @version $Revision: 12710 $ $Date: 2011-03-18 17:00:16 -0300 (Fri, 18 Mar 2011) $
 * @since 4.0.7
 */
public class ChannelAclSerializationManager extends AclSerializationManager implements IChannelAclSerializationManager
{
	// SERIALIZATION
	
	@Override
	public AccessControlListSerializableConfig serialize(SerializationContext serializationContext, String securableId) throws PortalException
	{
		// get acl
		AccessControlList accessControlList = ManagerFactory.getChannelAclManager().get(serializationContext.getSessionConfig(), securableId, serializationContext.getTransaction());

		// remove acl entries that reference global principals if necessary
		ManagerFactory.getChannelAclManager().checkRequiredPermissions(accessControlList);
		
		// serialize acl
		ManagerFactoryInternal.getSerializationManager2().serializeObject(accessControlList, serializationContext.getDirectory(), getConfigPath(serializationContext, accessControlList.getId()), configAlias, serializationContext.getDefaultSerializer());

		// analyze external dependencies
		ChannelConfig channelConfig = ManagerFactory.getChannelManager().get(serializationContext.getSessionConfig(), securableId, serializationContext.getTransaction());
		analyzeExternalDependencies(serializationContext, channelConfig, accessControlList);

		// build structure object
		return createSerializableConfig(serializationContext, accessControlList, AccessControlListSerializableConfig.AclType.CHANNEL, channelConfig.getId());
	}

	private void analyzeExternalDependencies(final SerializationContext serializationContext, final ChannelConfig channelConfig, final AccessControlList dependentAccessControlList) throws PortalException
	{
		ISerializationMessageConfigNodeBuilder serializationMessageConfigNodeBuilder = new ISerializationMessageConfigNodeBuilder()
		{
			public SerializationMessageConfigNode build() throws PortalException
			{
				return getSerializationMessageConfigNode(serializationContext, channelConfig, dependentAccessControlList);
			}
		};
		super.analyzeExternalDependencies(serializationContext, serializationMessageConfigNodeBuilder, dependentAccessControlList);
	}

	public SerializationMessageConfigNode getSerializationMessageConfigNode(BaseSerializationContext baseSerializationContext, ChannelConfig channelConfig, AccessControlList accessControlList) throws PortalException
	{
		return getSerializationMessageConfigNode(baseSerializationContext, channelConfig, accessControlList, EnumSet.of(RecursionStopPoints.None));
	}

	public SerializationMessageConfigNode getSerializationMessageConfigNode(BaseSerializationContext baseSerializationContext, ChannelConfig channelConfig, AccessControlList accessControlList, EnumSet<RecursionStopPoints> recursionStopPoints) throws PortalException
	{
		AccessControlListSerializationMessageConfig accessControListSerializationMessageConfig = new AccessControlListSerializationMessageConfig();
		accessControListSerializationMessageConfig.setId(accessControlList.getId());

		SerializationMessageConfigNode serializationMessageConfigNode = new SerializationMessageConfigNode();
		serializationMessageConfigNode.setSerializationMessageConfig(accessControListSerializationMessageConfig);
		if (!recursionStopPoints.contains(RecursionStopPoints.Channel))
			serializationMessageConfigNode.getParentNodes().add(ManagerFactoryInternal.getChannelSerializationManager().getSerializationMessageConfigNode(baseSerializationContext, channelConfig));
		serializationMessageConfigNode.setNavigateScript("window.parent.opener.open('../../../../"+PageConfig.PAGE_MAIN+"?"+PortalRequestParameters.PAGE_PARAMETER_PAGEID+"="+PageConfig.PAGE_ID_ADMIN_BLANK+"&"+PortalRequestParameters.PAGE_PARAMETER_CHANNELID+"=" + channelConfig.getId() + "&"+PortalRequestParameters.PAGE_PARAMETER_RUN_TIME_INTERFACE+"=lumis.service.portalmanagement.channelacl.administration&"+PortalRequestParameters.PAGE_PARAMETER_ITEMID+"=" + channelConfig.getId() + "','_blank', 'top=10,width=500,height=595,left=20');return false;");
		return serializationMessageConfigNode;
	}
	
	@Override
	protected IAclManager getAclManager()
	{
		return ManagerFactory.getChannelAclManager();
	}
}

